<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>B树与B+树：数据库索引结构的深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
        }
        h1, h2, h3 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-bg {
            background: linear-gradient(135deg, #3a7bd5 0%, #00d2ff 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .feature-icon {
            font-size: 2.5rem;
            color: #3a7bd5;
            margin-bottom: 1rem;
        }
        .highlight {
            background: linear-gradient(120deg, #f6d365 0%, #fda085 100%);
            background-repeat: no-repeat;
            background-size: 100% 40%;
            background-position: 0 90%;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-bg text-white py-20 lg:py-32 px-4">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl lg:text-6xl font-bold mb-6">B树与B+树</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">数据库索引核心技术的深度解析</p>
            <div class="max-w-3xl mx-auto">
                <p class="text-lg md:text-xl mb-8 leading-relaxed">
                    在数据库与文件系统领域，B树与B+树是两种经典的多路平衡搜索树结构。它们以高效的查询性能、优秀的磁盘I/O特性，成为现代存储系统的基石。
                </p>
            </div>
            <div class="flex justify-center space-x-4">
                <a href="#b-tree" class="px-6 py-3 bg-white text-blue-600 font-semibold rounded-full hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-tree mr-2"></i>B树详解
                </a>
                <a href="#b-plus-tree" class="px-6 py-3 bg-white text-blue-600 font-semibold rounded-full hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-project-diagram mr-2"></i>B+树解析
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- B-Tree Section -->
        <section id="b-tree" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-2 h-10 bg-blue-500 mr-4"></div>
                <h2 class="text-3xl font-bold">B-树的基本概念</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <p class="text-lg mb-6 leading-relaxed">
                        B-树（Balanced Tree）是一种自平衡的多叉树，通常用于数据库系统和文件系统。它的节点包含多个键和子树指针，可以有多个子节点。
                    </p>
                    <p class="text-lg mb-6 leading-relaxed">
                        B-树的每个节点都存储了键和值对，叶子节点也存储数据。在B-树中，所有节点的高度相同，这使得搜索、插入和删除操作的时间复杂度均为O(log n)。
                    </p>
                    <div class="bg-blue-50 p-6 rounded-lg border-l-4 border-blue-500">
                        <h4 class="font-bold text-blue-800 mb-2 flex items-center">
                            <i class="fas fa-lightbulb mr-2"></i>关键特性
                        </h4>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>所有节点都存储数据</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>平衡操作通过节点分裂与合并实现</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>适合内存受限环境下的高效查找</span>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="mermaid">
                            graph TD
                                A((10 20)) --> B((5 8))
                                A --> C((15 18))
                                A --> D((25 30))
                                B --> E((2 3 4))
                                B --> F((6 7))
                                B --> G((9))
                                C --> H((12 13))
                                C --> I((16 17))
                                C --> J((19))
                                D --> K((22 24))
                                D --> L((28 29))
                                D --> M((35 40))
                        </div>
                        <p class="text-sm text-gray-500 mt-4 text-center">B-树结构示例：所有节点都包含键和数据</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- B+Tree Section -->
        <section id="b-plus-tree" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-2 h-10 bg-green-500 mr-4"></div>
                <h2 class="text-3xl font-bold">B+树的基本概念</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <p class="text-lg mb-6 leading-relaxed">
                        B+树是B-树的一种变体，是一种更适合于磁盘存储和数据检索的树结构。与B-树不同，B+树的非叶子节点只存储键值和指向子节点的指针，不存储数据。
                    </p>
                    <p class="text-lg mb-6 leading-relaxed">
                        所有的实际数据都存储在叶子节点中，并且所有的叶子节点通过一个双向链表连接。这使得B+树在范围查询和顺序访问时性能更优。
                    </p>
                    <div class="bg-green-50 p-6 rounded-lg border-l-4 border-green-500">
                        <h4 class="font-bold text-green-800 mb-2 flex items-center">
                            <i class="fas fa-lightbulb mr-2"></i>核心优势
                        </h4>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>非叶子节点更紧凑，可存储更多键</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>叶子节点链表支持高效范围查询</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>更适合磁盘存储和大规模数据</span>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="mermaid">
                            graph TD
                                A[10 20] --> B[5 8]
                                A --> C[15 18]
                                A --> D[25 30]
                                B --> E((2 3 4))
                                B --> F((5 6 7))
                                B --> G((8 9))
                                C --> H((10 12 13))
                                C --> I((15 16 17))
                                C --> J((18 19))
                                D --> K((20 22 24))
                                D --> L((25 28 29))
                                D --> M((30 35 40))
                                E -.- F -.- G -.- H -.- I -.- J -.- K -.- L -.- M
                        </div>
                        <p class="text-sm text-gray-500 mt-4 text-center">B+树结构示例：非叶子节点仅含键，叶子节点链表连接</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section id="comparison" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-2 h-10 bg-purple-500 mr-4"></div>
                <h2 class="text-3xl font-bold">B+树与B-树的主要区别</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <h3 class="text-xl font-bold mb-4 text-blue-600 flex items-center">
                            <i class="fas fa-tree mr-2"></i>B-树特性
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 text-blue-800 flex items-center justify-center mr-3">1</div>
                                <span>所有节点都存储键和值对</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 text-blue-800 flex items-center justify-center mr-3">2</div>
                                <span>查询可能在非叶子节点结束</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 text-blue-800 flex items-center justify-center mr-3">3</div>
                                <span>节点数据分布导致可能有冗余</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 text-blue-800 flex items-center justify-center mr-3">4</div>
                                <span>树高度可能略高，I/O次数稍多</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 text-blue-800 flex items-center justify-center mr-3">5</div>
                                <span>叶子节点间无链接，顺序访问不便</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <h3 class="text-xl font-bold mb-4 text-green-600 flex items-center">
                            <i class="fas fa-project-diagram mr-2"></i>B+树特性
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-green-100 text-green-800 flex items-center justify-center mr-3">1</div>
                                <span>非叶子节点仅存储键和指针</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-green-100 text-green-800 flex items-center justify-center mr-3">2</div>
                                <span>查询必须到达叶子节点</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-green-100 text-green-800 flex items-center justify-center mr-3">3</div>
                                <span>无数据冗余，结构更紧凑</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-green-100 text-green-800 flex items-center justify-center mr-3">4</div>
                                <span>树高度通常更低，减少I/O</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-green-100 text-green-800 flex items-center justify-center mr-3">5</div>
                                <span>叶子节点链表支持高效范围查询</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="bg-gray-50 p-8 rounded-xl border border-gray-200">
                <div class="mermaid">
                    flowchart LR
                        A[关键比较维度] --> B[节点内容]
                        A --> C[查询性能]
                        A --> D[数据冗余]
                        A --> E[树高度]
                        A --> F[顺序访问]
                        
                        B --> B1[B-树: 所有节点存数据]
                        B --> B2[B+树: 仅叶子节点存数据]
                        
                        C --> C1[B-树: 可能在中间节点结束]
                        C --> C2[B+树: 必须到叶子节点]
                        
                        D --> D1[B-树: 可能有冗余]
                        D --> D2[B+树: 无冗余]
                        
                        E --> E1[B-树: 可能略高]
                        E --> E2[B+树: 通常更低]
                        
                        F --> F1[B-树: 顺序访问不便]
                        F --> F2[B+树: 链表支持高效遍历]
                </div>
            </div>
        </section>

        <!-- Application Section -->
        <section id="application" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-2 h-10 bg-orange-500 mr-4"></div>
                <h2 class="text-3xl font-bold">应用场景的选择</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                            <i class="fas fa-tree text-blue-500 text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-blue-600">B-树的适用场景</h3>
                    </div>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-blue-400 mt-1 mr-2"></i>
                            <span class="flex-1">需要频繁插入、删除操作的应用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-blue-400 mt-1 mr-2"></i>
                            <span class="flex-1">数据量相对较小的场景</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-blue-400 mt-1 mr-2"></i>
                            <span class="flex-1">内存数据库系统</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-blue-400 mt-1 mr-2"></i>
                            <span class="flex-1">需要快速随机访问的场景</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center mr-4">
                            <i class="fas fa-project-diagram text-green-500 text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-green-600">B+树的适用场景</h3>
                    </div>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-green-400 mt-1 mr-2"></i>
                            <span class="flex-1">数据库和文件系统等大规模存储</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-green-400 mt-1 mr-2"></i>
                            <span class="flex-1">磁盘I/O效率要求高的场景</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-green-400 mt-1 mr-2"></i>
                            <span class="flex-1">需要频繁范围查询的应用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-green-400 mt-1 mr-2"></i>
                            <span class="flex-1">需要顺序遍历数据的情况</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Key Takeaways -->
        <section class="bg-purple-50 p-8 rounded-xl border border-purple-200 mb-12">
            <h3 class="text-2xl font-bold text-purple-800 mb-6 flex items-center">
                <i class="fas fa-key mr-3"></i>关键要点总结
            </h3>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <div class="feature-icon">
                        <i class="fas fa-balance-scale"></i>
                    </div>
                    <h4 class="font-bold mb-2">结构差异</h4>
                    <p class="text-gray-700">
                        B-树所有节点存储数据，B+树仅叶子节点存储数据，非叶子节点更紧凑。
                    </p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <div class="feature-icon">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h4 class="font-bold mb-2">性能比较</h4>
                    <p class="text-gray-700">
                        B+树在范围查询和顺序访问上优势明显，B-树在随机访问上可能更优。
                    </p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <div class="feature-icon">
                        <i class="fas fa-database"></i>
                    </div>
                    <h4 class="font-bold mb-2">适用场景</h4>
                    <p class="text-gray-700">
                        B-树适合内存受限环境，B+树是磁盘存储和大规模数据的首选。
                    </p>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <a href="http://www.yuque.com/jtostring" class="hover:text-white transition duration-300">
                        http://www.yuque.com/jtostring
                    </a>
                </div>
                <div class="flex space-x-4">
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-blue-600 hover:text-white transition duration-300">
                        <i class="fab fa-github"></i>
                    </a>
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-blue-500 hover:text-white transition duration-300">
                        <i class="fab fa-twitter"></i>
                    </a>
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-red-500 hover:text-white transition duration-300">
                        <i class="fab fa-youtube"></i>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-sm text-center md:text-left">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>